<?php
using("Expression_Exception");
using("Utils_Reflection_Facade");
using("Utils_Resource_Manager");
using("Utils_Resource_Symbols");

/**
 * Construct operators
 *
 * @author Alexander Schearer <aas11@duke.edu>
 */
class Expression_Operator_Factory {
	
	/**
	 * Resource manager
	 *
	 * @var Utils_Resource_Manager
	 */
	private $_operators;
	
	/**
	 * Resource manager
	 *
	 * @var Utils_Resource_Manager
	 */
	private $_symbols;

	/**
	 * Initialize a new factory
	 *
	 * @throws Expression_Exception
	 */
	public function __construct() {
		try {
			$this->_operators = new Utils_Resource_Manager($this);
			$this->_symbols = new Utils_Resource_Symbols($this);
		} catch (Utils_Resource_Exception $e) {
			throw new Expression_Exception($e);
		}
	}
	
	/**
	 * Construct a new expression and return it
	 *
	 * @param String $symbol
	 * @param array $args
	 * @return Expression
	 */
	public function construct($symbol, $args) {
		try {
			return Utils_Reflection_Facade::getInstance($this->_operators->getString($symbol), 
				array($this->_symbols->getString($symbol), $args));
		} catch (Utils_Reflection_Exception $e) {
			throw new Expression_Exception($e);
		}
	}
	
	/**
	 * Retrieve a single instance of the factory
	 *
	 * @return Expression_Operator_Factory
	 */
	public static function getSingleton() {
		static $singleton;
		if (!isset($singleton)) {
			$singleton = new Expression_Operator_Factory();
		}
		return $singleton;
	}
}
?>